package com.retlife.tvshow.serial_port

import android.util.Log

/**
 * 串口回调
 */
abstract class BaseSerialReceiver {
    var dataBuffer: ByteArray = ByteArray(1024)
    var bufferLen: Int = 0
    fun startCheck() {}

    /**
     * 连接结果回调
     *
     * @param path   串口地址(当有多个串口需要统一处理时，可以用地址来区分)
     * @param isSucc 连接是否成功
     */
    abstract fun connectMsg(path: String?, isSucc: Boolean)

    /**
     * 读取到的数据回调
     *
     * @param bytes 读取到的数据
     * @param size  数据长度
     */
    fun readData(bytes: ByteArray, size: Int) {
        if (bufferLen + size > dataBuffer.size) {
            bufferLen = 0
            Log.e(javaClass.simpleName, "串口读取缓存满了")
        }
        if (size > 0) {
            System.arraycopy(bytes, 0, dataBuffer, bufferLen, size)
            bufferLen += size
            checkData()
        }
    }

    abstract fun checkData()
}
